

**************
*Defining programs to replicate BKR method

cap program drop BKR
program define BKR

quietly{
	
	preserve
	 gen land=exp(l)
	 gen labor=exp(x)
	 gen output=exp(y)

	bys HHID year season: egen L_hh=sum(land)
	bys HHID year season: egen X_hh=sum(labor)
	bys HHID year season: egen Y_hh=sum(output)

	
	  bys HHID year season: gen n=_n
	 keep if  n==1
		
	gen log_L= ln(L_hh)
	gen log_X= ln(X_hh)
	gen log_y=ln(Y_hh)
	
	 sort year season
	 egen period=group(year season)
	 
	 sort HHID
	 egen hhid_num=group(HHID)
	 
	 *lagged input levels
	 xtset hhid_num period

			
	global gamma = $alphal +  $alphax
	global alpha= $alphal / $gamma


	* Construct TFPR and composite input
	
  gen log_I =$alpha * log_L  + (1-$alpha )*log_X
  gen I=L_hh^$alpha *  X_hh^(1-$alpha )
  gen ln_TFPR= log_y - log_I
   keep if ln_TFPR!=.

   
      *trim 1% tails of TFPR each period
	 forvalues i=1/8{
			if $trim ==1  {
			cap	sum ln_TFPR if period==`i', de
			cap replace ln_TFPR=. if period==`i' & (ln_TFPR<`r(p1)' | ln_TFPR>`r(p99)')
			}
	 }
   
   
	*step 1
	***************
	
	*growth of compositve input and output
	
	sort hhid_num period
	gen DlogI=log_I - L.log_I
	gen Dlogy=log_y - L.log_y
   
	*cosstruct decile sof TFPR 
	
    * weighte average of ln(TFPR) by year
	bys period: egen mean_ln_TFPR=mean(ln_TFPR)
	
	* deviations from the mean
	gen dev_ln_TFPR = ln_TFPR -  mean_ln_TFPR


	*	 trim extreme vlues of TFPR growth (increase or decrase by a facrtor of 5 or more)
  	sort hhid_num period
  	gen D_dev_ln_TFPR= dev_ln_TFPR - L.dev_ln_TFPR
   	 drop if (D_dev_ln_TFPR>5 |  D_dev_ln_TFPR<-5 )& D_dev_ln_TFPR!=.

	 
	 
	*tornqvist average at household levels
	bys hhid_num: egen tornqvist_ln_TFPR=mean(dev_ln_TFPR)

	*create deciles of the tornqvist_ln_TFPR
	xtile deciles=tornqvist_ln_TFPR, n(10)




	*step 2
	***************
	
	*calculate beta_k
 
quietly  forvalues i=1/10 {
	 reg Dlogy      DlogI     i.period if   deciles==`i'
	 sum ln_TFPR if e(sample)
	 if `i'==1 {
	 	outreg2 using table_beta.xls, bdec(3)  replace addstat("mean lnTFPR", `r(mean)')
	 } 
	 else {
	 	outreg2 using table_beta.xls, bdec(3)   addstat("mean lnTFPR", `r(mean)')
	 }
	 global beta_`i'=_b[DlogI]
 
}


	*step 3
	***************
	
*adjsut due to compression of tornqvist ln TFRP
reg tornqvist_ln_TFPR dev_ln_TFPR
cap drop E_tornqvist_ln_TFPR
predict E_tornqvist_ln_TFPR, xb

*create cutoff values 
   forvalues i=1/10{
	 sum tornqvist_ln_TFPR if deciles==`i'
	 global cutoff_`i'=`r(max)'
	  
}
	
	
*assig betas 

cap drop beta_k
  gen beta_k=.
replace beta_k= $beta_1 if    E_tornqvist_ln_TFPR <=$cutoff_1
replace beta_k= $beta_2 if  E_tornqvist_ln_TFPR >$cutoff_1   & E_tornqvist_ln_TFPR <=$cutoff_2
replace beta_k= $beta_3 if  E_tornqvist_ln_TFPR >$cutoff_2   & E_tornqvist_ln_TFPR <=$cutoff_3
replace beta_k= $beta_4 if  E_tornqvist_ln_TFPR >$cutoff_3   & E_tornqvist_ln_TFPR <=$cutoff_4
replace beta_k= $beta_5 if  E_tornqvist_ln_TFPR >$cutoff_4   & E_tornqvist_ln_TFPR <=$cutoff_5
replace beta_k= $beta_6 if  E_tornqvist_ln_TFPR >$cutoff_5   & E_tornqvist_ln_TFPR <=$cutoff_6
replace beta_k= $beta_7 if  E_tornqvist_ln_TFPR >$cutoff_6   & E_tornqvist_ln_TFPR <=$cutoff_7
replace beta_k= $beta_8 if  E_tornqvist_ln_TFPR >$cutoff_7   & E_tornqvist_ln_TFPR <=$cutoff_8
replace beta_k= $beta_9 if  E_tornqvist_ln_TFPR >$cutoff_8  & E_tornqvist_ln_TFPR <=$cutoff_9
replace beta_k= $beta_10 if  E_tornqvist_ln_TFPR >$cutoff_9   & E_tornqvist_ln_TFPR <=$cutoff_10

 
 	*step 4
	***************
	
	
*generate ln(beta)
cap drop ln_beta_k
gen ln_beta_k=ln(beta_k)


*estimate vaaiance of tau
corr ln_TFPR ln_beta_k, cov
global var_tau= `r(Var_1)' + `r(cov_12)' 
 }
 
 di "var_tau  "  $var_tau
  di "var_TFPR  "  `r(Var_1)'
di "var_tau/ varTFPR is equal to   " (`r(Var_1)' + `r(cov_12)') /`r(Var_1)' 
restore
end




	
	
	
*Table_BKR (Uganda)
use "$FA_datasets/data_TFP_UGA.dta", clear


*Column 1

 quietly {
 	global alphal  = 0.69
 global alphax  = 0.22
 global trim =1
 }
 
 BKR 
 * Estimates of beta_k are in XLS  table_beta.xls
 
   
*Column 2
  quietly {
  global alphal  = 0.4
  global alphax  = 0.3 
  global trim =1
  }
  
 BKR 
  
  *Column 3
  quietly {
 global alphal  = 0.69
 global alphax  = 0.22
 global trim =0
  }
  
 BKR 
 
 *Column 4
  quietly {
  global alphal  = 0.4
  global alphax  = 0.3 
  global trim =0
  }
  
 BKR 
  
  
  
  
  *Table_BKR (tanzania)
use "$FA_datasets/data_TFP_TZ.dta", clear


*Column 1



  quietly {
 global alphal  = 0.61
 global alphax  = 0.26
 global trim =1
  }
  
 BKR 
   
 *Column 2
  quietly {
  global alphal  = 0.4
  global alphax  = 0.3 
  global trim =1
  }
  
 BKR 
  
     *Column 3
  quietly {
 	global alphal  = 0.61
 global alphax  = 0.26
 global trim =0
 }
 
 BKR 
 
 
*Column 4
  quietly {
  global alphal  = 0.4
  global alphax  = 0.3 
  global trim =0
  }
  
 BKR 


  
  
  
  
  
   
  
